package com.example.androidshixun.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    public static String getMD5(String text) {

        String md5 = "";

        try {
            MessageDigest digest = null;
            digest = MessageDigest.getInstance("md5");

            byte[] result = digest.digest(text.getBytes());

            for(byte b: result ) {
                //b转换成int
                int num = b & 0xff;
                String str = Integer.toHexString(num);
                if(str.length() == 1) {
                    str = "0"+str;
                }
                md5 = md5+str;
            }

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5;
    }
}
